package servlet;

import com.ncist.dao.MessagesDao;
import com.ncist.dao.yaoguaiDao;
import com.ncist.pojo.Messages;
import com.ncist.pojo.yaoguai;
import com.ncist.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@WebServlet("/yaoguai")
public class yaoguaiServlet extends HttpServlet {
    SqlSession sqlSession;
@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String func = request.getParameter("action");
        List<yaoguai> yaoguai ;
        sqlSession = MybatisUtils.getSqlSession();
        yaoguaiDao mapper = sqlSession.getMapper(yaoguaiDao.class);
        if(func == null){
            func ="";
        }
        if (func.equals("query")) {
            String keyword = request.getParameter("key");
             yaoguai = mapper.getByKey(keyword);
        }else {
            yaoguai = mapper.getAllyaoguai();
        }
    request.setAttribute("yaoguai",yaoguai);
    request.getRequestDispatcher("/list.jsp").forward(request,response);
        //第一步：获得sqlSession对象
        //第二步:获得Mapper接口的代理对象
        //第三步:执行方法
        //关闭sqlSession

        sqlSession.close();

    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
